home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1997 / MacHack 1997.toast / Hacks / Hacks ’96 / PredatorPrey / myActionProc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-22  |  5.4 KB  |  148 lines  |  [TEXT/KAHL]

  1. /************************************************************************************/
  2. /*    myActionProc            C14Calculator                                            */
  3. /*    Called when there is a mouse click on a control part.                            */
  4. /*                                                                                    */
  5. /*    Note:    although the physical location of destRect doesn't change, scrolling    */
  6. /*            and autoscroll change the value of destRect.top                            */
  7. /************************************************************************************/
  8.  
  9. #include "myActionProc.h"
  10. #ifndef __C14__
  11. #include    "PredatorPrey.h"
  12. #endif
  13. #include "WhichWindow.h"
  14. #include "HelpGetProc.h"
  15. #include "Globals.h"
  16. #include <stdlib.h>
  17.  
  18. void    myActionSeg()    {}
  19.  
  20. pascal void myActionProc(ControlHandle theControl, short partCode)
  21. {
  22.     int                MAPRetCode;
  23.     WindowPtr        theWindow;
  24.     TEHandle        theTE;
  25.     int                vView;                        /* height of viewRect rounded        */
  26.     int                vViewP;                        /* height of viewRect not rounded    */
  27.     int                vLine;                        /* height of one line of text        */
  28.     int                vTotal;                        /* total height of text                */
  29.     int                vAmount;                    /* total amount that can be scroled    */
  30.     int                vScrlAmt;                    /* scroll amount                    */
  31.     div_t            r;                            /* work area for division            */
  32.     int                tAdjust;                    /* amount to adjust thumb scrolling    */
  33.     int                teHSub;                        /* subscript of active TE item        */
  34.     short            j;
  35.  
  36.     teHSub = windTbl[windSub].windTEHSub;                    /* set the subscript    */
  37.                                             /*    windTEHSub was set in HelpGetProc.c    */
  38.     MAPRetCode = TRUE;
  39.     
  40.     theWindow = ((WindowPtr) (**theControl).contrlOwner);        /* ptr to window    */
  41.     WhichWindow (theWindow, &windSub);                            /* subscr to table    */
  42.     theTE = windTbl[windSub].windTEH[teHSub];                    /* handle to TE rec    */
  43.  
  44.     vLine = (**theTE).lineHeight;                                /* height of 1 line    */
  45.     vViewP = (**theTE).viewRect.bottom -(**theTE).viewRect.top;    /* height of 1 page    */
  46.     vView = (vViewP / vLine) * vLine;                            /* rounded to lines    */
  47.     vTotal = (**theTE).nLines * (**theTE).lineHeight;            /* height whole txt    */
  48.     if ((**theTE).teLength > 0                                     /* if ends in c/r    */
  49.         && (*((**theTE).hText))[(**theTE).teLength-1]=='\r')    /*    adjust for it    */
  50.             vTotal += (**theTE).lineHeight;
  51.  
  52.     switch (partCode)
  53.         {
  54.         case (inUpButton):
  55.             vScrlAmt = ((**theTE).viewRect.top > (**theTE).destRect.top + vLine)
  56.                     ? vLine
  57.                     : (**theTE).viewRect.top - (**theTE).destRect.top;
  58.             if (vScrlAmt < 0)                    /* if calculation has wrong sign    */
  59.                 vScrlAmt = 0;                    /* don't scroll                        */
  60.         break;
  61.                 
  62.         case (inPageUp):
  63.             vScrlAmt = ((**theTE).viewRect.top > (**theTE).destRect.top + vView)
  64.                     ? vView
  65.                     : (**theTE).viewRect.top - (**theTE).destRect.top;
  66.             if (vScrlAmt < 0)                    /* if calculation has wrong sign    */
  67.                 vScrlAmt = 0;                    /* don't scroll                        */
  68.         break;
  69.         
  70.         case (inPageDown):
  71.             vScrlAmt = ((**theTE).viewRect.bottom 
  72.                         < (**theTE).destRect.top + vTotal - vView) 
  73.                     ? -vView 
  74.                     : -((**theTE).destRect.top + vTotal - (**theTE).viewRect.bottom);
  75.             if (vScrlAmt > 0)                    /* if calculation has wrong sign    */
  76.                 vScrlAmt = 0;                    /* don't scroll                        */
  77.         break;
  78.                 
  79.         case (inDownButton):
  80.             vScrlAmt = ((**theTE).viewRect.bottom 
  81.                         < (**theTE).destRect.top + vTotal - vLine) 
  82.                     ? -vLine 
  83.                     : -((**theTE).destRect.top + vTotal - (**theTE).viewRect.bottom);
  84.             if (vScrlAmt > 0)                    /* if calculation has wrong sign    */
  85.                 vScrlAmt = 0;                    /* don't scroll                        */
  86.         break;
  87.         
  88.         case (inThumb):
  89.                                                 /* current value of control            */
  90.             j = GetCtlValue (theControl);        /*   after moving thumb                */
  91.             
  92.                                                 /* difference in control value        */
  93.             vScrlAmt = kkk;                        /*   due to moving thumb            */
  94.             
  95.                                                 /* how much more or less than whole    */
  96.             r = div (vScrlAmt, vLine);            /*   number of lines in the scroll?    */
  97.             
  98.                                                 /* compute adjustment to the scroll    */
  99.                                                 /*   amount and the control value    */
  100.                                                 /*   to result in only scrolling a    */
  101.                                                 /*   whole number of lines            */
  102.                                                 
  103.             if (fabs(r.rem) < vLine/2)                /* if we should round down        */
  104.                 tAdjust = -r.rem;                    /*   use this                    */
  105.             else                                    /* else                            */
  106.                 {                                    /* if we should round up        */
  107.                 if (r.rem > 0)                        /*   if we are scrolling up        */
  108.                     tAdjust = -r.rem + vLine;        /*        use this                */
  109.                 else                                /*   else if scrolling down        */
  110.                     tAdjust = -r.rem - vLine;        /*      use this                */
  111.                 }
  112.                     
  113.             vScrlAmt += tAdjust;                /* adjust scroll amt (scroll later)    */
  114.             
  115.             if (tAdjust != 0)                    /* adjust control amount and redraw    */
  116.                 SetCtlValue (theControl, (j - tAdjust));
  117.         break;
  118.         
  119.         case (0):
  120.             vScrlAmt = 0;
  121.                                                 /* If the total vertical length of    */
  122.                                                 /* the text now is less than the    */
  123.                                                 /* height of the viewRect, prepare    */
  124.                                                 /* to scroll to the top of viewRect    */
  125.             if ((vViewP >= vTotal)
  126.                     && ((**theTE).viewRect.top != (**theTE).destRect.top))
  127.                 vScrlAmt = (**theTE).viewRect.top - (**theTE).destRect.top;    
  128.         break;
  129.         
  130.         default:
  131.             return /*MAPRetCode*/;
  132.         }
  133.         
  134.     TEScroll (0, vScrlAmt, theTE);                            /* do the scroll        */            
  135.  
  136.     vAmount = (vTotal > vViewP) ? vTotal-vViewP : 0;        /* ..max scrollable amt    */
  137.     if (vAmount == 0)                                        /* ..if view > text        */
  138.         (**theTE).destRect.top = (**theTE).viewRect.top;    /* ..  fit all in view    */
  139.     
  140.     if (GetCtlMax (theControl) != vAmount)                    /* set max to..            */
  141.         SetCtlMax(theControl, vAmount);                        /* .. max scrollable    */
  142.         
  143.     if (partCode != inThumb)                                /* reset and redraw        */
  144.         SetCtlValue (theControl, ((**theTE).viewRect.top) - (**theTE).destRect.top);
  145.     
  146.     return    /*MAPRetCode*/; 
  147. }
  148.